package com.bingxue.edu.management.education.controller;

import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.education.model.query.ParentQuery;
import com.bingxue.edu.management.education.model.req.ParentReq;
import com.bingxue.edu.management.education.model.req.ParentUpdateReq;
import com.bingxue.edu.management.education.model.req.RecommenderUpdateReq;
import com.bingxue.edu.management.education.model.resp.ParentDetailResp;
import com.bingxue.edu.management.education.model.resp.ParentResp;
import com.bingxue.edu.management.education.service.ParentService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 家长账号
 *
 * @author zq
 * @since 2024/08/19
 */
@Tag(name = "家长账号接口")
@RestController
@RequestMapping("/education/parent")
@RequiredArgsConstructor
public class ParentAccountController {

    private final ParentService parentService;

    @Operation(summary = "分页查询家长信息")
//    @ApiResource("system:org:manage")
    @GetMapping("/page")
    public Response<Pagination<ParentResp>> page(ParentQuery query, @Validated PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, pageQuery);
        Page<ParentResp> page = parentService.pageAs(pageQuery.toPage(), queryWrapper, ParentResp.class);
        return Response.success(Pagination.of(page));
    }

    @Operation(summary = "查询家长详情")
//    @ApiResource("system:org:manage")
    @GetMapping("/detail/{id}")
    public Response<ParentDetailResp> detail(@PathVariable String id) {
        return Response.success(parentService.getDetailById(id));
    }

    @Operation(summary="创建家长信息")
    @ApiResource("system:org:manage")
    @PostMapping("/create")
    public Response<Void> create(@RequestBody @Validated ParentReq req) {
        parentService.create(req);
        return Response.success();
    }

    @Operation(summary="更新家长信息")
    @ApiResource("system:org:manage")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody @Validated ParentUpdateReq req, @PathVariable String id) {
        parentService.update(req, id);
        return Response.success();
    }

    @Operation(summary = "设置推荐人")
    @ApiResource("edu:student:manage")
    @PostMapping("/set-recommender/{parentId}")
    public Response<Void> setRecommender(@RequestBody @Validated RecommenderUpdateReq req, @PathVariable String parentId) {
        parentService.setRecommender(parentId,req.getRecommenderPhone());
        return Response.success();
    }
}
